#!/usr/bin/python
import cgi
import cgitb
import os
from pygallib import usersession
from pygallib import layoutlib
from pygallib import albumlib
from pygallib import userlib
from pygallib import picturelib

# Enable debug print
cgitb.enable()

# Printing Headers
logged_in = usersession.logged_in()

headers = {}
headers["Content-type"] = "text/html"
layoutlib.print_http_headers(headers)
layoutlib.print_page_open("PyGallery - Home")
print("<body>")
menu = []
menu.append(("Home", "gallery.cgi"))

user = None
if logged_in:
  user = usersession.get_session_user()

layoutlib.print_page_header(menu, user)
print('<div id="page_body">')

# Get the fields from url
fields = cgi.FieldStorage()

if logged_in:
  user = usersession.get_session_user()
  if "album_name" in fields:
    album_name = fields["album_name"].value
    if len(album_name)==0:
      print('<p>Error: Album had no name</p>')
    else:
      is_public = int(fields["is_public"].value)
      # Add that album to albums table
      new_album_id = albumlib.add_album(user["user_id"], album_name, is_public)
      print('<p>Album <a href="album.cgi?id=%d">%s</a> created, click ' 
          % (new_album_id, album_name) +
          '<a href="gallery.cgi">here</a> for the gallery.')

print('<h1>Create new album:</h1>')
print("""
<form action="create_album.cgi" 
  method="post" enctype="multipart/form-data">
  <ul>
    <li>
      <label for="album_name">Name:</label>
      <input type="text" maxlength="512" name="album_name" size="30"/>
    </li>
    <li>
      <label for="is_public">Visibility:</label>
      <input type="radio" name="is_public" 
        value="1" checked="checked">public</input>
      <input type="radio" name="is_public" value="0">private</input>
    </li>
    <li>
      <input type="submit" value="Submit" />
    </li>
  </ul>
</form>
""")
print('</div>')
layoutlib.print_page_close()
